package cn.beatuty.makeup.module.system.controller.admin.article;

import cn.beatuty.makeup.module.system.controller.admin.article.vo.ArticlePageReqVO;
import cn.beatuty.makeup.module.system.controller.admin.article.vo.ArticleRespDetailVO;
import cn.beatuty.makeup.module.system.controller.admin.article.vo.ArticleRespVO;
import cn.beatuty.makeup.module.system.controller.admin.article.vo.ArticleSaveReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.article.ArticleDO;
import cn.beatuty.makeup.module.system.service.article.ArticleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;

import cn.beatuty.makeup.framework.common.pojo.PageParam;
import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.common.pojo.CommonResult;
import cn.beatuty.makeup.framework.common.util.object.BeanUtils;
import static cn.beatuty.makeup.framework.common.pojo.CommonResult.success;

import cn.beatuty.makeup.framework.excel.core.util.ExcelUtils;

import cn.beatuty.makeup.framework.operatelog.core.annotations.OperateLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.beatuty.makeup.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 文章")
@RestController
@RequestMapping("/app/article")
@Validated
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建文章")
    public CommonResult<Long> createArticle(@Valid @RequestBody ArticleSaveReqVO createReqVO) {
        return success(articleService.createArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章")
    public CommonResult<Boolean> updateArticle(@Valid @RequestBody ArticleSaveReqVO updateReqVO) {
        articleService.updateArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteArticle(@RequestParam("id") Long id) {
        articleService.deleteArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ArticleRespVO> getArticle(@RequestParam("id") Long id) {
        ArticleDO article = articleService.getArticle(id);
        return success(BeanUtils.toBean(article, ArticleRespVO.class));
    }

    @GetMapping("/get/mobile")
    @Operation(summary = "手机获得文章详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ArticleRespDetailVO> getArticleDetal(@RequestParam("id") Long id) {
        ArticleRespDetailVO article = articleService.getArticleDetail(id);
        return success(BeanUtils.toBean(article, ArticleRespDetailVO.class));
    }

    @GetMapping("/count")
    @Operation(summary = "统计文章数量")
    public CommonResult<Long> getArticleCount() {
        Long count = articleService.getArticleCount();
        return success(count);
    }

    @GetMapping("/trend")
    @Operation(summary = "统计趋势")
    public CommonResult<List<Map<String, Object>>> getArticlePublishTrend() {
        return success(articleService.countTotalPriceByDayInLast7Days());
    }

    @PostMapping("/page")
    @Operation(summary = "获得文章分页")
    public CommonResult<PageResult<ArticleRespVO>> getArticlePage(@Valid @RequestBody ArticlePageReqVO pageReqVO) {
        if (pageReqVO.getIsAllData()){
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
            pageReqVO.setStatus(String.valueOf(2));
        }
        PageResult<ArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章 Excel")
    @OperateLog(type = EXPORT)
    public void exportArticleExcel(@Valid ArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleDO> list = articleService.getArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章.xls", "数据", ArticleRespVO.class,
                        BeanUtils.toBean(list, ArticleRespVO.class));
    }

}